package com.apes.fn.scm.retailOrder.service.retailProcess.process;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.service.retailProcess.CommonRetailDataProcess;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.soe.metadata.Scene;
import com.apes.framework.plugin.soe.store.database.repository.SceneRepository;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.MapUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 年卡(yearcard)、兑换券置换(replacement)
 *
 * @author wuq
 * @create 2021-05-21 16:59
 */
@Service
public class CouponProcess extends CommonRetailDataProcess {

    @Override
    public void beforeSave(RetailOrder retailOrder) {
        // 更新场景流，防止传过来的数据有误，增加一次逻辑判断
        beforeProcess(retailOrder, this::updateScene);
    }

    private void updateScene(RetailOrder retailOrder){
        if (retailOrder.getAmtTotal() > 0) {
            Scene scene = SpringManager.getBean(SceneRepository.class).findOne("RO001");
            retailOrder.setScene(scene);
        } else {
            //  跨公司置换时，就不写凭证了，写入的凭证也是一笔金额为 0的，没有意义
            //  负置换时，不能走场景流，需要单据去生成一张销售退回凭证
            retailOrder.setScene(null);
        }
    }

    @Override
    public void afterSave(RetailOrder retailOrder) {

        // 这里是负置换时，需要走这里
        if (retailOrder.getAmtTotal() < 0) {
            retailOrder.setDateDone(new Date());
            retailOrder.setPostDate(new Date());
            invoke("retail.coupon.returnVoucher", MapUtil.mapped(retailOrder));
        }
    }

    @Override
    public boolean support(String orderType) {
        return LogicalUtil.in(orderType, "replacement", "yearcard");
    }
}
